<!--
  组件说明写于此处!
-->
<template>
  <div class="nav-frame" :style="{ 'order': data.info.order }">
    <div
      :class="['nav-node',expanded ? '' : 'nav-node-parent']"
      @click.stop="hasChild ? expanded=!expanded : $emit('click',data,$event);"
      @dblclick="$emit('dblclick',data,$event)"
      @contextmenu.prevent="$emit('contextmenu',data,$event)"
    >
      <Icon
        v-if="iconShow"
        class="nav-node-icon"
        :data="data"
        :title="data.info ? data.info.hint : data.name"
      />
      <span
        v-if="titleShow"
        class="nav-node-text"
      >{{ data.name }}</span>
      <!-- <i v-if="hasChild" :class="['nav-node-icon fa',expanded ? 'fa-angle-up' : 'fa-angle-down']" /> -->
    </div>
    <div v-if="hasChild" v-show="expanded" class="nav-node-child">
      <NavigationNode
        v-for="(node, index) in data.children"
        :key="index"
        :data="node"
        :icon-show="iconShow"
        @click="$emit('click',$event)"
        @dblclick="$emit('dblclick',$event)"
        @contextmenu="contextmenu"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'NavigationNode',
  components: {
    Icon: () => import('@components/Icon.vue')
  },
  props: {
    data: {
      type: Object,
      default: () => { }
    },
    flag: {
      type: Boolean,
      default: () => false
    },
    iconShow: {
      type: Boolean,
      default: () => true
    },
    titleShow: {
      type: Boolean,
      default: () => true
    }
  },
  data() {
    return {
      expanded: this.flag
    }
  },
  computed: {
    hasChild() {
      return this.data.children && this.data.children.length > 0
    }
  },
  watch: {
    flag(newVal, oldVal) {
      this.expanded = newVal
    }
  },
  methods: {
    contextmenu(data, event) {
      this.$emit('contextmenu', data, event)
    }
  }
}
</script>
<style scoped>
.nav-frame{
  display: flex;
  /* flex-flow: column nowrap; */
}
.nav-node {
  display: flex;
  flex-flow: row nowrap;
  padding: 1rem;
}
.nav-node-parent{
  flex-grow:1;
}
.nav-node-child .nav-node {
  padding-left: 2rem;
}
.nav-node:hover{
  background-color: rgba(255, 255, 255, 0.1) !important;
}
</style>
